package cn.itcast.wushangxiantu.Activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.itcast.wushangxiantu.BaseAdapter.FuBenAdapter;
import cn.itcast.wushangxiantu.MessageEvent;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import cn.itcast.wushangxiantu.UserData.HuoDeNumber;
import cn.itcast.wushangxiantu.UserData.UserFuBenYuLan;
import cn.itcast.wushangxiantu.UserData.UserFuBenZhanDou;
import cn.itcast.wushangxiantu.UserData.UserValue;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class FuBen extends AppCompatActivity implements View.OnClickListener {

    private TextView tvJinRu;
    private Spinner snDengJi;
    private TextView tvBiaoTi;
    private TextView tvDiFang;
    private TextView tvWofang;
    private LinearLayout flSenLin;
    private ListView lvXinXi;
    private TextView tvZhanDou;
    private TextView tvHuoDe;
    private LinearLayout ll;
    private RelativeLayout rl;
    private TextView tvBuyu;
    private View View1;
    private TextView tvCaiKuang;
    private View View2;
    private TextView tvFaMu;
    private View View3;
    private TextView tvCaiJi;
    private View View4;
    private RadioButton rbtn1;
    private RadioButton rbtn2;
    private RadioButton rbtn3;
    private TextView tvXianYu;
    private TextView tvShiTou;
    private TextView tvMuTou;
    private TextView tvZaoCao;
    private UserValue value;
    int dengji = 9;//怪物等级
    int zhuangbei = 1;//选择装备
    int number = 100;//倍数
    FuBenAdapter fuBenAdapter;
    List<UserFuBenZhanDou> lists = new ArrayList<>();
    private boolean working = true;
    private Button btnShuoMing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fu_ben);
        initView();
        getYuLan();
    }

    private void getYuLan() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + Login.getUser().getUsernumber()
                + "&need=fubenyulan", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("yulan1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("yulan2", src);
                UserFuBenYuLan user = null;
                try {
                    user = new Gson().fromJson(src, UserFuBenYuLan.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 1;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 1:
                    UserFuBenYuLan userFuBenYuLan = (UserFuBenYuLan) message.obj;
                    if (userFuBenYuLan != null) {
                        tvXianYu.setText("咸鱼：" + userFuBenYuLan.getXianyu() + "");
                        tvShiTou.setText("石头：" + userFuBenYuLan.getShitou() + "");
                        tvMuTou.setText("木头：" + userFuBenYuLan.getMutou() + "");
                        tvZaoCao.setText("杂草：" + userFuBenYuLan.getZacao() + "");
                    } else {
                        ToastUtil.toast(FuBen.this, "服务器坏掉了");
                    }
                    break;
                case 2:
                    UserFuBenZhanDou userFuBenZhanDou = (UserFuBenZhanDou) message.obj;
                    if (userFuBenZhanDou != null) {
                        if (userFuBenZhanDou.getMoney() != 0) {
                            lists.add((UserFuBenZhanDou) message.obj);
                            if (fuBenAdapter == null) {
                                fuBenAdapter = new FuBenAdapter(FuBen.this);
                            }
                            lvXinXi.setAdapter(fuBenAdapter);
                            fuBenAdapter.setList(lists);
                            //发送信息
                            if (lists.get(lists.size() - 1).getShihun() != 0) {
                                MessageEvent messageEvent = new MessageEvent("xinxi");
                                messageEvent.setContent("恭喜玩家" + Login.getUser().getName() + "在遗迹获得石魂*" + lists.get(lists.size() - 1).getShihun());
                                EventBus.getDefault().post(messageEvent);
                            }
                            if (!lists.get(lists.size() - 1).getZhuangbei().trim().equals("")) {
                                MessageEvent messageEvent = new MessageEvent("xinxi");
                                messageEvent.setContent("恭喜玩家" + Login.getUser().getName() + "在遗迹获得装备：" + lists.get(lists.size() - 1).getZhuangbei());
                                EventBus.getDefault().post(messageEvent);
                            }
                            if (lists.get(lists.size() - 1).getSuipian() != 0) {
                                MessageEvent messageEvent = new MessageEvent("xinxi");
                                messageEvent.setContent("恭喜玩家" + Login.getUser().getName() + "在遗迹获得残页：" + lists.get(lists.size() - 1).getSuipian());
                                EventBus.getDefault().post(messageEvent);
                            }
                            getYuLan();
                        } else if (userFuBenZhanDou.getStw().trim().equals("NO1")) {
                            ToastUtil.toast(FuBen.this, "怪物吹口气你飞了");
                        } else if (userFuBenZhanDou.getStw().trim().equals("NO2")) {
                            ToastUtil.toast(FuBen.this, "30回合过去了你已筋疲力尽");
                        } else {
                            ToastUtil.toast(FuBen.this, userFuBenZhanDou.getStw());
                        }
                    } else {
                        ToastUtil.toast(FuBen.this, "服务器坏掉了");
                    }
                    break;
            }
            return false;
        }
    });

    private void initView() {
        tvJinRu = (TextView) findViewById(R.id.tvJinRu);
        snDengJi = (Spinner) findViewById(R.id.snDengJi);
        tvBiaoTi = (TextView) findViewById(R.id.tvBiaoTi);
        tvDiFang = (TextView) findViewById(R.id.tvDiFang);
        tvWofang = (TextView) findViewById(R.id.tvWofang);
        flSenLin = (LinearLayout) findViewById(R.id.flSenLin);
        lvXinXi = (ListView) findViewById(R.id.lvXinXi);
        tvZhanDou = (TextView) findViewById(R.id.tvZhanDou);
        tvHuoDe = (TextView) findViewById(R.id.tvHuoDe);
        ll = (LinearLayout) findViewById(R.id.ll);
        rl = (RelativeLayout) findViewById(R.id.rl);
        tvBuyu = (TextView) findViewById(R.id.tvBuyu);
        View1 = (View) findViewById(R.id.View1);
        tvCaiKuang = (TextView) findViewById(R.id.tvCaiKuang);
        View2 = (View) findViewById(R.id.View2);
        tvFaMu = (TextView) findViewById(R.id.tvFaMu);
        View3 = (View) findViewById(R.id.View3);
        tvCaiJi = (TextView) findViewById(R.id.tvCaiJi);
        View4 = (View) findViewById(R.id.View4);
        rbtn1 = (RadioButton) findViewById(R.id.rbtn1);
        rbtn2 = (RadioButton) findViewById(R.id.rbtn2);
        rbtn3 = (RadioButton) findViewById(R.id.rbtn3);
        tvXianYu = (TextView) findViewById(R.id.tvXianYu);
        tvShiTou = (TextView) findViewById(R.id.tvShiTou);
        tvMuTou = (TextView) findViewById(R.id.tvMuTou);
        tvZaoCao = (TextView) findViewById(R.id.tvZaoCao);

        tvJinRu.setOnClickListener(this);
        tvBuyu.setOnClickListener(this);
        tvCaiJi.setOnClickListener(this);
        tvFaMu.setOnClickListener(this);
        tvCaiKuang.setOnClickListener(this);
        tvZhanDou.setOnClickListener(this);
        tvHuoDe.setOnClickListener(this);
        value = new UserValue(Login.getUser());
        int num = 9;
        tvDiFang.setText("敌方\n生命值：" + num * 80 + "\t\t\t防御值：" + (int) num * 12 + "\n" +
                "攻击力：" + num * 8 + "\t\t\t暴击率：" + (0) + "\n" +
                "恢复：" + num * 1 + "\t\t\t穿透：" + num * 1 + "\n" +
                "命中：" + 0 + "\t\t\t闪避：" + 0);
        tvWofang.setText("我方\n生命值：" + value.getSm() + "\t\t\t防御值：" + value.getFy() + "\n" +
                "攻击力：" + value.getGj() + "\t\t\t暴击率：" + value.getBj() + "\n" +
                "恢复：" + value.getHf() + "\t\t\t穿透：" + value.getCt() + "\n" +
                "命中：" + value.getMz() + "\t\t\t闪避：" + value.getSb());
        btnShuoMing = (Button) findViewById(R.id.btnShuoMing);
        btnShuoMing.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvJinRu:
                tvBiaoTi.setText(snDengJi.getSelectedItem().toString());
                int num = snDengJi.getSelectedItemPosition() * 10 + 9;
                ToastUtil.toast(FuBen.this, "已进入" + num + "级" + tvBiaoTi.getText().toString());
                String content = "";
                dengji = num;
                if (num <= 11) {
                    int gail = (int) (num * 0.3);
                    content = "敌方\n生命值：" + num * 80 + "\t\t\t防御值：" + (int) num * 2 + "\n" +
                            "攻击力：" + num * 8 + "\t\t\t暴击率：" + (gail) + "\n" +
                            "恢复：" + num * 1 + "\t\t\t穿透：" + num * 1 + "\n" +
                            "命中：" + gail + "\t\t\t闪避：" + gail;
                } else if (num <= 21) {
                    int gail = (int) (num * 0.3);
                    content = "敌方\n生命值：" + num * 109 + "\t\t\t防御值：" + (int) num * 6 + "\n" +
                            "攻击力：" + num * 12 + "\t\t\t暴击率：" + (gail) + "\n" +
                            "恢复：" + num * 3 + "\t\t\t穿透：" + num * 3 + "\n" +
                            "命中：" + gail + "\t\t\t闪避：" + gail;
                } else if (num <= 31) {
                    int gail = (int) (num * 0.3);
                    content = "敌方\n生命值：" + num * 150 + "\t\t\t防御值：" + (int) num * 15 + "\n" +
                            "攻击力：" + num * 30 + "\t\t\t暴击率：" + (gail) + "\n" +
                            "恢复：" + num * 7 + "\t\t\t穿透：" + num * 7 + "\n" +
                            "命中：" + gail + "\t\t\t闪避：" + gail;
                } else if (num <= 41) {
                    int gail = (int) (num * 0.3);
                    content = "敌方\n生命值：" + num * 165 + "\t\t\t防御值：" + (int) num * 18 + "\n" +
                            "攻击力：" + num * 37 + "\t\t\t暴击率：" + (gail) + "\n" +
                            "恢复：" + num * 8 + "\t\t\t穿透：" + num * 8 + "\n" +
                            "命中：" + gail + "\t\t\t闪避：" + gail;
                } else if (num <= 51) {
                    int gail = (int) (num * 0.3);
                    content = "敌方\n生命值：" + num * 175 + "\t\t\t防御值：" + (int) num * 20 + "\n" +
                            "攻击力：" + num * 40 + "\t\t\t暴击率：" + (gail) + "\n" +
                            "恢复：" + num * 10 + "\t\t\t穿透：" + num * 10 + "\n" +
                            "命中：" + gail + "\t\t\t闪避：" + gail;
                } else if (num <= 61) {
                    int gail = (int) (num * 0.4);
                    content = "敌方\n生命值：" + num * 180 + "\t\t\t防御值：" + (int) num * 22 + "\n" +
                            "攻击力：" + num * 44 + "\t\t\t暴击率：" + (gail) + "\n" +
                            "恢复：" + num * 11 + "\t\t\t穿透：" + num * 11 + "\n" +
                            "命中：" + gail + "\t\t\t闪避：" + gail;
                } else if (num <= 81) {
                    int gail = (int) (num * 0.4);
                    content = "敌方\n生命值：" + num * 200 + "\t\t\t防御值：" + (int) num * 30 + "\n" +
                            "攻击力：" + num * 60 + "\t\t\t暴击率：" + (gail) + "\n" +
                            "恢复：" + num * 15 + "\t\t\t穿透：" + num * 15 + "\n" +
                            "命中：" + gail + "\t\t\t闪避：" + gail;
                } else {
                    int gail = (int) (num * 0.5);
                    content = "敌方\n生命值：" + num * 210 + "\t\t\t防御值：" + num * 32 + "\n" +
                            "攻击力：" + num * 65 + "\t\t\t暴击率：" + gail + "\n" +
                            "恢复：" + num * 16 + "\t\t\t穿透：" + num * 16 + "\n" +
                            "命中：" + gail + "\t\t\t闪避：" + gail;
                }
                tvDiFang.setText(content);
                break;
            case R.id.tvBuyu:
                changeMenu(0);
                zhuangbei = 1;
                break;
            case R.id.tvCaiKuang:
                zhuangbei = 2;
                changeMenu(1);
                break;
            case R.id.tvFaMu:
                zhuangbei = 3;
                changeMenu(2);
                break;
            case R.id.tvCaiJi:
                zhuangbei = 4;
                changeMenu(3);
                break;
            case R.id.tvZhanDou:
                String neirong = "";
                switch (zhuangbei) {
                    case 1:
                        neirong = "咸鱼";
                        break;
                    case 2:
                        neirong = "石头";
                        break;
                    case 3:
                        neirong = "木头";
                        break;
                    case 4:
                        neirong = "杂草";
                        break;
                }
                if (rbtn1.isChecked()) {
                    number = 1000;
                } else if (rbtn2.isChecked()) {
                    number = 5000;
                } else if (rbtn3.isChecked()) {
                    number = 10000;
                }
                GeneralDialog("准备战斗", "当前选择为" + tvBiaoTi.getText().toString().trim() + neirong + "\n倍数为：" + number, 1);
                break;
            case R.id.tvHuoDe:
                List<HuoDeNumber> list = new ArrayList<>();
                list.add(new HuoDeNumber("金币", 0));
                list.add(new HuoDeNumber("石魂", 0));
                list.add(new HuoDeNumber("经验", 0));
                list.add(new HuoDeNumber("技能残页", 0));
                for (int i = 0; i < lists.size(); i++) {
                    if (lists.get(i).getStw().equals("OK")) {
                        list.get(2).setNumber(list.get(2).getNumber() + lists.get(i).getJy());
                        list.get(0).setNumber(list.get(0).getNumber() + lists.get(i).getMoney());
                        if (!lists.get(i).getZhuangbei().equals("")) {
                            list.add(new HuoDeNumber(lists.get(i).getZhuangbei(), 1));
                        }
                        if (lists.get(i).getShihun() != 0) {
                            list.get(1).setNumber(list.get(1).getNumber() + lists.get(i).getShihun());
                        }
                        if (lists.get(i).getSuipian() != 0) {
                            list.get(3).setNumber(list.get(3).getNumber() + lists.get(i).getSuipian());
                        }
                    }
                }
                String xinxi = "";
                for (int i = 0; i < list.size(); i++) {
                    xinxi += list.get(i).getName() + "*" + list.get(i).getNumber() + "\n";
                }
                GeneralDialog("掉落详情", xinxi, 0);
                break;
            case R.id.btnShuoMing:
                GeneralDialog("遗迹介绍", "消耗物资开启遗迹，击杀魔物可获得大量修为，金币，概率性获得石魂，技能残页，魔物概率性还会掉落随身装备", 0);
                break;
        }
    }

    public void changeMenu(int num) {
        View1.setVisibility(View.INVISIBLE);
        View2.setVisibility(View.INVISIBLE);
        View3.setVisibility(View.INVISIBLE);
        View4.setVisibility(View.INVISIBLE);

        switch (num) {
            case 0:
                View1.setVisibility(View.VISIBLE);
                break;
            case 1:
                View2.setVisibility(View.VISIBLE);
                break;
            case 2:
                View3.setVisibility(View.VISIBLE);
                break;
            case 3:
                View4.setVisibility(View.VISIBLE);
                break;
        }
    }

    public void GeneralDialog(String head, String content, final int Num) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle(head)

                .setMessage(content)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        if (Num == 1) {
                            new Thread() {
                                @Override
                                public void run() {
                                    try {
                                        sleep(2000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    if (working) {
                                        getZhanDou();
                                    }
                                }
                            }.start();
                        }
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        working = false;
    }

    private void getZhanDou() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + Login.getUser().getUsernumber()
                + "&number=" + number + "&gwdj=" + dengji + "&id=" + zhuangbei + "&need=fubenzhandou", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("zhandou1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("zhandou2", src);
                UserFuBenZhanDou user = null;
                try {
                    user = new Gson().fromJson(src, UserFuBenZhanDou.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 2;
                handler.sendMessage(message);
            }
        });
    }
}